package cn.shengchao.examstar.ai.domain.valueobject;

import lombok.Getter;

import java.util.Objects;

/**
 * 掌握程度值对象
 */
@Getter
public class DegreeOfMastery {
    
    public static final DegreeOfMastery EXPERT = new DegreeOfMastery("S", "精通");
    public static final DegreeOfMastery MASTER = new DegreeOfMastery("A", "掌握");
    public static final DegreeOfMastery UNDERSTAND = new DegreeOfMastery("B", "理解");
    public static final DegreeOfMastery BEGINNER = new DegreeOfMastery("C", "初步了解")

            ;
    
    private final String code;
    private final String description;
    
    private DegreeOfMastery(String code, String description) {
        this.code = code;
        this.description = description;
    }
    
    /**
     * 根据代码创建掌握程度
     */
    public static DegreeOfMastery fromCode(String code) {
        if (code == null) {
            throw new IllegalArgumentException("掌握程度代码不能为空");
        }
        
        switch (code.toUpperCase()) {
            case "S":
                return EXPERT;
            case "A":
                return MASTER;
            case "B":
                return UNDERSTAND;
            case "C":
                return BEGINNER;
            default:
                return BEGINNER;
        }
    }
    
    /**
     * 判断是否比另一个掌握程度更高
     */
    public boolean isHigherThan(DegreeOfMastery other) {
        return getLevel() > other.getLevel();
    }
    
    /**
     * 判断是否比另一个掌握程度更低
     */
    public boolean isLowerThan(DegreeOfMastery other) {
        return getLevel() < other.getLevel();
    }
    
    private int getLevel() {
        switch (code) {
            case "S": return 4;
            case "A": return 3;
            case "B": return 2;
            case "C": return 1;
            default: return 0;
        }
    }
    
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        DegreeOfMastery that = (DegreeOfMastery) o;
        return Objects.equals(code, that.code);
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(code);
    }
    
    @Override
    public String toString() {
        return description + "(" + code + ")";
    }
}